<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Member extends FOX_Controller
{

    function __construct()
    {

        parent::__construct();
        $this->load->model('areas_m');
        $this->load->model('user_m');
        $this->load->model('money_m');
        $this->load->model('find_m');
        $this->load->model('api_order_m');
        $this->load->model('api_balance_m');
        $this->load->model('userCategory_m');
        $this->load->model('attachs_m');
        $this->load->model('api_good_m');
        $this->load->model('api_favorite_m');
        $this->load->model('goodVouchers_m');
        $this->load->model('freeVouchers_m');
        $this->load->model('dliver_m');
        $this->load->model('recommend_m');
        $this->load->model('authority_m');
        $this->load->library('pagination');

        if (!$this->session->userdata('user_id')) {
            redirect(site_url('user/login'));
        }
    }

    public function index()
    {
        $data['foot_menu'] = 'member';
        $user_id           = $this->session->userdata('user_id');
        $data['user_info'] = $this->user_m->get_user_by_user_id($user_id);
        $this->load->view('user', $data);
    }

    public function address($order_no = "")
    {
        $user_id = $this->session->userdata('user_id');
        $dress   = $this->member_m->get_address_by_uid($user_id);
        if (!empty($dress)) {
            foreach ($dress as $key => $val) {
                $area                      = $this->areas_m->get_area_by_id($val['dress_province']);
                $area2                     = $this->areas_m->get_area_by_id($val['dress_city']);
                $area3                     = $this->areas_m->get_area_by_id($val['dress_district']);
                $dress[$key]['dress_city'] = $area['area_name'] . $area2['area_name'] . $area3['area_name'];
            }
        }
        if (empty($order_no) && empty($this->session->userdata('back_url'))) {
            $back_url = site_url('member');
        } else {
            if ($this->session->userdata('order_back_url')) {
                $back_url = $this->session->userdata('order_back_url');
            } else {
                $back_url = site_url('order/order_other/' . $order_no);
                $this->session->set_userdata('order_back_url', $back_url);
            }
        }
        $data['back_url'] = $back_url;
        $data['dress']    = $dress;
        $this->load->view('user_address', $data);
    }

    public function choose_addr()
    {

        $user_id = $this->session->userdata('user_id');
        $dress   = $this->member_m->get_address_by_uid($user_id);
        if (!empty($dress)) {
            foreach ($dress as $key => $val) {
                $area                      = $this->areas_m->get_area_by_id($val['dress_province']);
                $area2                     = $this->areas_m->get_area_by_id($val['dress_city']);
                $area3                     = $this->areas_m->get_area_by_id($val['dress_district']);
                $dress[$key]['dress_city'] = $area['area_name'] . $area2['area_name'] . $area3['area_name'];
            }
        }
        $data['select'] = 1;
        $data['dress']  = $dress;
        $this->load->view('user_address', $data);
    }

    public function addressadd()
    {
        $province         = $this->areas_m->get_sons_by_pid();
        $data['province'] = $province;
        $this->config->load('cityjson');
        // pp($data);exit;
        $this->load->view('address_add', $data);
    }

    public function updatedress()
    {
        $post    = $this->input->post();
        $user_id = $this->session->userdata('user_id');
        if ($user_id) {
            if ($post['dress_utel'] && !checkMobile($post['dress_utel'])) {
                // echo json_encode(['code' => 4, 'msg' => '号码格式不正确']);exit();
                show_message('号码格式不正确!', site_url('member/address'), 1);
                exit;
            }
            if (!$post['dress_uname'] && !$post['hd_area'] && !$post['dress_utel'] && !$post['dress_udress']) {
                show_message('输入信息不完整，请重新输入', site_url('member/address'), 1);
            } else {
                $data['user_id'] = $user_id;
                // var_dump($post);exit;
                $id                   = isset($post['dress_id']) ? intval($post['dress_id']) : false;
                $arr                  = explode(',', $post['hd_area']);
                $data['dress_uname']  = isset($post['dress_uname']) ? $post['dress_uname'] : "";
                $data['dress_udress'] = isset($post['dress_udress']) ? $post['dress_udress'] : "";

                $data['dress_province'] = isset($arr[0]) ? $arr[0] : "";
                $data['dress_city']     = isset($arr[1]) ? $arr[1] : "";
                $data['dress_district'] = isset($arr[2]) ? $arr[2] : "";
                $data['dress_utel']     = isset($post['dress_utel']) ? $post['dress_utel'] : "";
                $data['dress_type']     = isset($post['dress_type']) ? intval($post['dress_type']) : 0;
                $data['dress_add_time'] = time();
            }

            // pp($data);exit;
            if ($id) {
                $citys = $this->member_m->sava_dress($id, $data);
            } else {
                //查询是否存在该用户的地址
                if ($this->member_m->get_dress_by_user_id($user_id)) {
                    $data['dress_type'] = 0;
                } else {
                    $data['dress_type'] = 1;
                }
                $citys = $this->member_m->add_dress($data);
            }
            if ($citys) {
                if ($this->session->userdata('gourl')) {
                    redirect($this->session->userdata('gourl'));
                } else {
                    redirect(site_url('member/address'));
                }
            } else {
                redirect(site_url('member/address'));
            }
        }
    }

    public function balance()
    {
        $user_id               = $this->session->userdata('user_id');
        $data['userinfo']      = $this->user_m->get_user_by_user_id($user_id);
        $money                 = $this->api_balance_m->get_balance_list(0, $this->config->item('page_list_balance'), $user_id, 'desc');
        $data['money']         = $money;
        $data['count_balance'] = $this->api_balance_m->count_balance($user_id);
        // 充值和支出
        $user_zhichu           = $this->member_m->user_zhichu($user_id);
        $data['user_zhichu']   = $user_zhichu['zhichu'] ?: "0.00";
        $user_chongzhi         = $this->member_m->user_chongzhi($user_id);
        $data['user_chongzhi'] = $user_chongzhi['chongzhi'] ?: "0.00";
        $this->load->view('user_balance', $data);
        // $this->load->view('user_balance');
    }

    public function integral()
    {
        $user_id                = $this->session->userdata('user_id');
        $data['userinfo']       = $this->user_m->get_user_by_user_id($user_id);
        $integral               = $this->api_balance_m->get_integral_list(0, $this->config->item('page_list_balance'), $user_id, 'desc');
        $data['count_integral'] = $this->api_balance_m->count_integral($user_id);
        $data['month_integral'] = $this->member_m->month_user_credit($user_id);
        $data['integral']       = $integral;
        $this->load->view('user_integral', $data);
    }

    public function site()
    {
        $user_id          = $this->session->userdata('user_id');
        $data['userinfo'] = $this->user_m->get_user_by_user_id($user_id);
        $dress            = $this->member_m->get_dress_by_user_id($user_id);
        // pp($dress);exit;
        $area1                          = $this->areas_m->get_area_by_id($dress['dress_province']);
        $area2                          = $this->areas_m->get_area_by_id($dress['dress_city']);
        $area3                          = $this->areas_m->get_area_by_id($dress['dress_district']);
        $data['userinfo']['dress_city'] = $area1['area_name'] . $area2['area_name'] . $area3['area_name'] . $dress['dress_udress'];
//         pp($data);exit;
        $this->load->view('user_info', $data);
    }

    public function recording()
    {
        $user_id = $this->session->userdata('user_id');
        $this->load->model('crond_m');
        $user_orders = $this->crond_m->all_find_orders_user_id($user_id);
        if ($user_orders) {
            foreach ($user_orders as $key => $value) {
                $this->crond_m->update_order_status($value['order_id']);
            }
        }
        $order_status = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        // pp($order_status);exit;
        // if($order_status == 1){}
        // $data['recording_count'] = $this->api_order_m->count_recordings_count_by_user_id($user_id);
        $orders = $this->api_order_m->get_recording_list(0, $this->config->item('page_list_recording'), $user_id, 'desc', $order_status);
        // pp($orders);exit;
        // $orders = $this->member_m->get_all_orders_by_user_id($user_id);
        $data['count_orders'] = $this->api_order_m->count_recordings_count_by_user_id($user_id, $order_status);
        if (!empty($orders)) {
            foreach ($orders as $key => $val) {
                $goods                      = $this->member_m->get_goods_name_by_id($val['good_id']);
                $orders[$key]['good_title'] = $goods['good_title'];
                $orders[$key]['good_price'] = $goods['good_price'];
                //获取图片
                $pic_lists = $this->attachs_m->get_one_attachs_by($goods['good_cover']);
                if ($pic_lists) {
                    $orders[$key]['good_cover'] = get_jianlou_image($pic_lists['file_location'], $pic_lists['file_name'], "small");
                } else {
                    $orders[$key]['good_cover'] = base_url('uploads/goods/nopic.jpg');
                }
            }
        }
        $data['orders']       = $orders;
        $data['order_status'] = $order_status;
        $this->load->view('user_recording', $data);
    }

    public function order()
    {
        $user_id              = $this->session->userdata('user_id');
        $orders               = $this->api_order_m->get_orders_list(0, $this->config->item('page_list_order'), $user_id, 'desc');
        $data['count_orders'] = $this->api_order_m->count_shop_orders_count_by_good_id_success($user_id);
        if (!empty($orders)) {
            foreach ($orders as $key => $val) {
                $goods                      = $this->member_m->get_goods_name_by_id($val['good_id']);
                $orders[$key]['good_title'] = $goods['good_title'];
                $orders[$key]['good_price'] = $goods['good_price'];
                //获取图片
                $pic_lists = $this->attachs_m->get_one_attachs_by($goods['good_cover']);
                if ($pic_lists) {
                    $orders[$key]['good_cover'] = get_jianlou_image($pic_lists['file_location'], $pic_lists['file_name'], "small");
                } else {
                    $orders[$key]['good_cover'] = base_url('uploads/goods/nopic.jpg');
                }
            }
        }
        $data['orders'] = $orders;
        $this->load->view('user_order', $data);
    }

    public function ordershow()
    {
        $user_id              = $this->session->userdata('user_id');
        $order_id             = $this->uri->segment(3);
        $order                = $this->member_m->get_order_by_order_id($order_id);
        $data['order']        = $order;
        $data['good']         = $this->member_m->get_goods_name_by_id($order['good_id']);
        $dress                = $this->member_m->get_adress_by_user_id($user_id);
        $area                 = $this->areas_m->get_area_name_by_id($dress['dress_province']);
        $area1                = $this->areas_m->get_area_name_by_id($dress['dress_city']);
        $area2                = $this->areas_m->get_area_name_by_id($dress['dress_district']);
        $data['dress_udress'] = $area['area_name'] . $area1['area_name'] . $area2['area_name'] . $dress['dress_udress'];
        // pp($dress);exit;
        $data['dress'] = $dress;
        $goods         = $this->member_m->get_goods_name_by_id($order['good_id']);
        //获取图片
        $pic_lists = $this->attachs_m->get_one_attachs_by($goods['good_cover']);
        if ($pic_lists) {
            $data['good_cover'] = get_jianlou_image($pic_lists['file_location'], $pic_lists['file_name'], "small");
        } else {
            $data['good_cover'] = base_url('uploads/goods/nopic.jpg');
        }
        $this->load->view('user_order_show', $data);
    }

    public function order_logistics($order_id)
    {
        $user_id = $this->session->userdata('user_id');
        //查询订单信息
        $order_info = $this->order_m->get_shop_orders_by_id($order_id);
        if (!empty($order_info)) {
            $deliver_info = $this->dliver_m->get_delivers_by_order_id($order_id);
            if (!empty($deliver_info)) {
                $order_no        = $order_info['order_no'];
                $deliver_code    = $deliver_info['deliver_code'];
                $deliver_no      = $deliver_info['deliver_no'];
                $deliver_company = $deliver_info['deliver_company'];
                $this->load->library('deliver');
                $deliver_info                    = $this->deliver->getOrderTracesByJson($order_no, $deliver_no, $deliver_code);
                $deliver_info                    = json_decode($deliver_info, true);
                $deliver_info['deliver_company'] = $deliver_company;
                // $this->response_json(1,"物流信息获取成功",$data['deliver_info']);exit();
            }
        }
        $data['deliver_info'] = $deliver_info;
        // pp($data);exit;
        $this->load->view('user_order_logistics', $data);
    }

    public function chang_img()
    {
        $config['upload_path']   = FCPATH . '/uploads/avatar/';
        $config['allowed_types'] = 'gif|jpg|png|JPG|PNG|JPEG|GIF|bmp|jpeg';
        $config['encrypt_name']  = true;
        $config['max_size']      = '30000';
        $uid                     = $this->session->userdata('user_id');
        $url                     = 'member/site/' . $uid;
        if ($_FILES['user_avatar']['name'] == '') {
            show_message('请选择图片', site_url($url));
        }
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('user_avatar')) {
            pp($this->upload->display_errors());
            exit();
        } else {
            //upload sucess
            $img_array = $this->upload->data();
            $this->load->library('ImageResize');
            if ($this->imageresize->resize_avatar($img_array['full_path'])) {
                $data = [
                    'user_avatar' => base_url('uploads/avatar/' . $this->imageresize->get_dir($uid) . $uid . '_avatar_big.png'),
                ];
                $this->user_m->update_user($uid, $data);
                //删除tmp下的原图
                unlink($img_array['full_path']);
                $this->session->set_userdata('user_avatar', $data['user_avatar']);
                redirect(site_url('member/site'), 1);
            } else {
                //设置三个头像没有成功
                redirect('member/index');
            }
        }
        // redirect(site_url('member/index'));
    }

    public function recharge()
    {
        $data['openid'] = !empty($this->session->userdata("wechat_user")['id']) ? $this->session->userdata("wechat_user")['id'] : "0";
        $this->load->view('user_recharge', $data);
    }

    //提现
    public function withdraw()
    {
        $user_id = $this->session->userdata('user_id');
        //用户余额里的全部金额
        $data['money']    = $this->money_m->get_all_money_by_id($user_id);
        $data['user_tel'] = $this->user_m->get_user_tel_by_id($user_id);
        $this->load->view('user_withdraw', $data);
    }

    public function bind_mobile()
    {

        $this->load->view('bind_mobile', []);
    }

    public function certified()
    {

        $uid       = $this->session->userdata('user_id');
        $authority = $this->authority_m->get_users_authority_by_id($uid);
        if (empty($authority)) {
            $this->authority_m->add_users_authority(["user_id" => $uid]);
        } else {

        }
        $data['authority'] = $authority;
        $this->load->view('certified', $data);
    }

    public function certified_img()
    {
        if (!empty($_FILES)) {
            if (isset($_FILES['card1'])) {
                $do_upload_name = "card1";
            }
            if (isset($_FILES['card2'])) {
                $do_upload_name = "card2";
            }
            if (isset($_FILES['bill1'])) {
                $do_upload_name = "bill1";
            }
            if (isset($_FILES['bill2'])) {
                $do_upload_name = "bill2";
            }
        }
        $config['upload_path']   = './uploads/avatar';
        $config['allowed_types'] = 'gif|jpg|png|JPG|PNG|JPEG|GIF|bmp|jpeg';
        $config['encrypt_name']  = true;
        $config['max_size']      = '10000';
        $uid                     = $this->session->userdata('user_id');
        $url                     = 'member/certified/' . $uid;
        if ($_FILES[$do_upload_name]['name'] == '') {
            show_message('请选择图片', site_url($url));
        }
        $uptype  = ['jpg', 'jpeg', 'png', 'bmp', 'png'];
        $torrent = explode(".", $_FILES[$do_upload_name]['name']);
        $fileend = strtolower(end($torrent));
        if (!in_array($fileend, $uptype)) {
            show_message('图片类型不正确', site_url($url));
        }

        $this->load->library('upload', $config);
        if (!$this->upload->do_upload($do_upload_name)) {
            pp($this->upload->display_errors());
            exit();
        } else {
            //upload sucess
            $img_array = $this->upload->data();
            $this->load->library('ImageResize');
            if ($this->imageresize->resize_certified($img_array['full_path'], $do_upload_name)) {
                $data = [
                    '' . $do_upload_name . '' => base_url('uploads/avatar/' . $this->imageresize->get_dir($uid) . $uid . '_' . $do_upload_name . '_big.png'),
                ];
                $this->authority_m->sava_users_authority($uid, $data);
                //删除tmp下的原图
//                unlink($img_array['full_path']);
                redirect(site_url('member/certified'), 1);
            } else {
                //设置三个头像没有成功
                redirect('member/certified');
            }
        }
    }

    public function sub_certified()
    {
        $uid    = $this->session->userdata('user_id');
        $result = $this->authority_m->sava_users_authority($uid, ['is_submit' => 1, 'status' => 1]);
        if ($result) {
            echo json_encode(['code' => 200, 'msg' => '提交成功']);
            exit();
        } else {
            echo json_encode(['code' => 400, 'msg' => '提交失败，请稍后再试']);
            exit();
        }
    }

    public function verified()
    {
        $uid       = $this->session->userdata('user_id');
        $user_info = $this->user_m->get_user_by_user_id($uid);
        if ($_POST) {
            $str['user_uname']     = $this->input->post('user_uname');
            $str['user_card']      = $this->input->post('user_card');
            $str['user_card_rz']   = 1;
            $str['user_card_time'] = time();
            $result                = $this->user_m->update_user($uid, $str);
            if ($result) {
                echo json_encode(['status' => 1, "info" => "认证资料提交成功"]);
                exit();
            } else {
                echo json_encode(['status' => 2, "info" => "失败，请稍后再试"]);
                exit();
            }
        }
        $data['user_info'] = $user_info;
        $data['foot_memu'] = 'user';
        $this->load->view('user_verified', $data);
    }

    public function user_loan($page = 1)
    {
        $uid = $this->session->userdata('user_id');
        $this->load->model('moneylogs_m');
        $limit                      = 30;
        $config['uri_segment']      = 4;
        $config['use_page_numbers'] = true;
        $config['base_url']         = site_url('member/user_load/');
        $config['total_rows']       = $this->db->where("user_id", $uid)->count_all('user_money_sale');
        $config['per_page']         = $limit;
        $config['num_links']        = 10;
        $this->pagination->initialize($config);
        $start              = ($page - 1) * $limit;
        $data['pagination'] = $this->pagination->create_links();
        $logs               = $this->moneylogs_m->get_all_money_logs($start, $limit, $uid);
        $data['logs']       = $logs;
        $member             = $this->user_m->get_user_by_user_id($uid);
        $data['member']     = $member;
//        pp($data);exit();
        $this->load->view('user_loan', $data);
    }

    public function kefu()
    {

        $data['foot_menu'] = "kefu";
        $this->load->view('service', $data);
    }

    public function recommend()
    {
        if ($_POST) {
            $post = $this->input->post();
            if (empty($post['mobile'])) {
                echo json_encode(['status' => 2, "info" => "请填写推荐手机号"]);
                exit();
            }
            $user = $this->user_m->is_mobile_registered($post['mobile']);
            if($user){
                echo json_encode(['status' => 2, "info" => "该号码已经注册过平台了"]);
                exit();
            }
            $recommend = $this->recommend_m->get_recommend_by_mobile($post['mobile']);
            if($recommend){
                echo json_encode(['status' => 2, "info" => "该号码已经被推荐过了"]);
                exit();
            }
            $uid = $this->session->userdata('user_id');
            $this->load->model('recommend_m');
            $data   = [
                'user_id'     => $uid,
                'name'      => $post['name'] ? $post['name'] : "",
                'mobile'      => $post['mobile'],
                'create_time' => date("Y-m-d H:i:s", time()),
                'state'       => 0,
            ];
            $result = $this->recommend_m->add_recommend($data);
            if ($result) {
                echo json_encode(['code' => 1, "info" => "恭喜，推荐成功！"]);
                exit();
            } else {
                echo json_encode(['code' => 2, "info" => "网络异常，请稍后再试"]);
                exit();
            }
        }
        $this->load->set_front_theme('wap');
        $data['viewmulu']  = base_url('public/wap/');
        $data['foot_memu'] = 'home';
        $this->load->view('recommend', $data);
    }

    public function invite()
    {
        $this->load->set_front_theme('wap');
        $data['viewmulu']  = base_url('public/wap/');
        $data['foot_memu'] = 'home';
        $this->load->view('invite', $data);
    }


    public function finance($type = 0)
    {
        // 获取简介
        $uid = $this->session->userdata('user_id');
        $this->load->model('page_m');
        $page = $this->page_m->get_pages_by_uid($uid,$type);
        $data['page'] = $page;
        // pp($data);
        // exit();
        $this->load->view('finance', $data);
    }
}
